<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // map(映射) 有返回值 返回一个新数组 不会改变原数组 
        var arr = ["张三", "李四", "王五"];
        // --------------原生方法-------------------------------------------
        var res = arr.map(function (item, key, arr) {
            return item;
        })
        console.log(res);
        // --------------用for底层来写-------------------------------------------
        function myMap(arr, cb) {
            var result = [];
            for (var i = 0; i < arr.length; i++) {
                result[i] = cb(arr[i], i, arr);
            }
            return result;
        }
        var res = myMap(arr, function (item, key, arr) {
            return item;
        })
        console.log(res);
        // --------------挂在到原型上-------------------------------------------
        Array.prototype.myMap = function (cb) {
            var result = [];
            for (var i = 0; i < this.length; i++) {
                result[i] = cb(this[i], i, this);
            }
            return result;
        }
        var res = arr.myMap(function (item, key, arr) {
            return item;
        })
        console.log(res);
    </script>
</body>
</html>